package com.zzyl.serve.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        /*现在还存在一个问题,是禁用启用状态和*/
//        1.先拿到护理计划信息,使用copyProperties对属性进行复制
        NursingPlan nursingPlan = nursingPlanMapper.selectNursingPlanById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
//        查询对应项目计划的列表,拿到plan的id然后去查询中间表的plan_id
        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProjectPlan::getPlanId, id);
        List<NursingProjectPlan> projectPlans = nursingProjectPlanMapper.selectList(wrapper);
//      如果存在护理计划项目计划列表,则进行封装
        if (!CollectionUtils.isEmpty(projectPlans)) {
//        2.再拿到其中的护理计划项目计划列表进行封装返回
//            查询对应中间表中的护理计划项目计划列表
            wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(NursingProjectPlan::getPlanId, id);
            List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(wrapper);
            List<NursingProjectPlanVo> nursingProjectPlanVos = new ArrayList<>();
//            把nursingProjectPlans其中的值复制给nursingProjectPlanVo
            nursingProjectPlans.stream().forEach(nursingProjectPlan -> {
                NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
                nursingProjectPlanVo.setProjectId(String.valueOf(nursingProjectPlan.getProjectId()));
                nursingProjectPlanVo.setId(nursingProjectPlan.getId());
                nursingProjectPlanVo.setPlanId(nursingProjectPlan.getPlanId());
                nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
                nursingProjectPlanVo.setExecuteCycle(String.valueOf(nursingProjectPlan.getExecuteCycle()));
                nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency() + ""));
                nursingProjectPlanVos.add(nursingProjectPlanVo);
            });
            nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        }
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan)
    {
        boolean flag = save(nursingPlan);
//        删除缓存
        deleteCache();
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlan nursingPlan)
    {
        boolean flag = updateById(nursingPlan);
//        删除缓存
        deleteCache();
        return flag? 1 : 0;
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids)
    {
        boolean flag = removeByIds(Arrays.asList(ids));
//        删除缓存
        deleteCache();
        return flag? 1 : 0;
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id)
    {
        boolean flag = removeById(id);
//        删除缓存
        deleteCache();
        return  flag? 1 : 0;
    }

    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }

    @Override
    public List<NursingPlan> planList() {
        List<NursingPlan> nursingPlans = (List<NursingPlan>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (CollectionUtils.isNotEmpty(nursingPlans)){
//            System.out.println("我查的是缓存哦~~~~~~");
            return nursingPlans;
        }
        LambdaQueryWrapper<NursingPlan> wrapper = new LambdaQueryWrapper();
        wrapper.eq(NursingPlan::getStatus, 1);
        List<NursingPlan> list = nursingPlanMapper.selectList(wrapper);
//        将查询数据添加到缓存中
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
